package com.gimi.cloud.bbp.merchantZone.consumer.service.product.impl;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gimi.cloud.bbp.merchantZone.consumer.feign.SkuFeignService;
import com.gimi.cloud.bbp.merchantZone.consumer.service.product.SkuService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.JsonUtil;
import com.gimi.cloud.bbp.common.utils.reqhead.Response;
import com.gimi.cloud.bbp.common.utils.reqhead.Util;
import com.gimi.cloud.bbp.dto.product.ProductSkuDto;


/**
 * 商品sku变价,修改保存
 * @author chendi
 * @date 2017年12月26日
 */
@Service
public class SkuServiceImpl implements SkuService {
	private static Logger logger = LoggerFactory.getLogger(SkuServiceImpl.class);
	private final SkuFeignService skuFeignService;

    @Autowired
    public SkuServiceImpl(SkuFeignService skuFeignService) {
		this.skuFeignService = skuFeignService;
	}

	@Override
	public void listSku(String requestData, HttpServletRequest request, HttpServletResponse response) throws MsgException {
		logger.info("srcData:" + requestData);
		ProductSkuDto dto = JsonUtil.jsonToBean(requestData, ProductSkuDto.class);
		
		if(StringUtils.isNotEmpty(dto.getProdId())) {
    		Util.sendResponse(skuFeignService.listSku(dto), response);
    	}else {
    		logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
    	}
	}

	@Override
	public void updateSku(String requestData, HttpServletRequest request, HttpServletResponse response) throws Exception{
		logger.info("srcData:" + requestData);
		List<ProductSkuDto> skuList = JSONObject.parseArray(JSONObject.parseObject(requestData).getString("skuData"), ProductSkuDto.class);
		
		//校验sku修改后的数据的合法性
		for (ProductSkuDto productSkuDto : skuList) {
			String prodId = productSkuDto.getProdId();
			Response respStatus = JsonUtil.jsonToBean(skuFeignService.getProductStatus(prodId), Response.class);
			//判断sku修改的参数合法性
			//价格,库存大于零, 状态为未上架
			//System.out.println(skuFeignService.getProductStatus(prodId)+"==============================");
			if (respStatus == null ||respStatus.getRes_data() == null|| !"2".equals(respStatus.getRes_data())) {
				logger.error("此商品状态不支持修改!");
				throw new MsgException(ResponseCodeEnum.SKU_NOT_CHANGE);
				
			}
			if (productSkuDto.getSkuPrice().doubleValue() <= 0  || productSkuDto.getSkuStock() < 0 ) {
				logger.error("输入参数错误");
				throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"输入参数错误");
			}
		}
		
		Util.sendResponse(skuFeignService.saveSku(skuList), response);
	}
	@Override
	public void deleteSku(String requestData, HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("srcData:" + requestData);
		ProductSkuDto dto = JsonUtil.jsonToBean(requestData, ProductSkuDto.class);
		
		if(StringUtils.isNotEmpty(dto.getSkuId())) {
    		Util.sendResponse(skuFeignService.deleteSku(dto), response);
    	}else {
    		logger.error("参数错误");
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
    	}
	}
	@Override
	public void skuImg(String requestData, HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.info("srcData:" + requestData);
		ProductSkuDto dto = JsonUtil.jsonToBean(requestData, ProductSkuDto.class);
		if(StringUtils.isNotEmpty(dto.getSkuId())) {
			Util.sendResponse(skuFeignService.skuImg(dto), response);
		}else {
			logger.error("参数错误");
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
	}

	@Override
	public void SkuModelDetail(String requestData, HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.info("srcData:" + requestData);
		ProductSkuDto dto = JsonUtil.jsonToBean(requestData, ProductSkuDto.class);
		if(StringUtils.isNotEmpty(dto.getSkuValue())) {
			Util.sendResponse(skuFeignService.SkuModelDetail(dto), response);
		}else {
			logger.error("参数错误");
			throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
		}
	}
	
	
	
	
	//测试代码
    public static void main(String[] args) {
    	ArrayList<ProductSkuDto> listjosn = new ArrayList<>();
    	ProductSkuDto p1 = new ProductSkuDto();
    	p1.setProdId("1");
    	ProductSkuDto p2 = new ProductSkuDto();
    	p1.setProdId("2");
    	listjosn.add(p1);
    	listjosn.add(p2);
    	String json = JSON.toJSONString(listjosn);
    	
    	String json2 = "[\r\n" + 
    			"        {\r\n" + 
    			"            \"skuId\":\"b61d74fff8ca4431a4e2643080d6e4a6\",\r\n" + 
    			"            \"prodId\":\"09273252110001\",\r\n" + 
    			"            \"skuPrice\":3333,\r\n" + 
    			"            \"skuStock\":3333,\r\n" + 
    			"            \"skuValue\":\"1,2,3\"\r\n" + 
    			"        },\r\n" + 
    			"        {\r\n" + 
    			"            \"skuId\":\"c0280f23c28c4e08a89bd8eddff13d7c\",\r\n" + 
    			"            \"prodId\":\"09273252110001\",\r\n" + 
    			"            \"skuPrice\":333,\r\n" + 
    			"            \"skuStock\":333,\r\n" + 
    			"            \"skuValue\":\"1,2,3\"\r\n" + 
    			"        },\r\n" + 
    			"        {\r\n" + 
    			"            \"skuId\":\"cf7e2dfd6f854c21b8768285d1099839\",\r\n" + 
    			"            \"prodId\":\"09273252110001\",\r\n" + 
    			"            \"skuPrice\":444,\r\n" + 
    			"            \"skuStock\":444,\r\n" + 
    			"            \"skuValue\":\"1,2,3\"\r\n" + 
    			"        }\r\n" + 
    			"    ]";
    	
    	List<ProductSkuDto> list = JSONObject.parseArray(json2, ProductSkuDto.class);
    	
		System.out.println(list);
	}
    
}
